package com.cos.hospital.ui.main;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cos.hospital.R;
import com.cos.hospital.code.Logic;
import com.cos.hospital.dao.HelperInfoDb;
import com.cos.hospital.dao.beandao.DbInfo;
import com.cos.hospital.model.UHttp;
import com.cos.hospital.model.bean.ResultBean;
import com.cos.hospital.model.bean.user.UserInfoBean;
import com.cos.hospital.model.connector.HttpMsgListener;
import com.cos.hospital.tool.MyUtil;
import com.cos.hospital.tool.SPUtil;
import com.cos.hospital.tool.UDialog;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.tool.glide.UGlide;
import com.cos.hospital.ui.usercenter.AssistantMyActivity;
import com.cos.hospital.ui.usercenter.MyMsgActivity;
import com.cos.hospital.ui.usercenter.SchedulingActivity;
import com.cos.hospital.ui.usercenter.UserCodeActivity;
import com.cos.hospital.ui.usercenter.UserInfoActivity;
import com.cos.hospital.widget.ProgressDialog;
import com.google.gson.JsonObject;
import com.jude.utils.JUtils;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

/**
 * @Description: 用户中心
 * @Author: Kosmos
 * @Date: 2018年09月11日 22:29
 * @Email: KosmoSakura@foxmail.com
 */
public class UserCenterFragment extends MainFragment {
    @BindView(R.id.user_bg)
    ImageView iBg;
    @BindView(R.id.user_avatar)
    ImageView iAvatar;
    @BindView(R.id.user_name)
    TextView tName;
    @BindView(R.id.user_job)
    TextView tJob;//职位
    @BindView(R.id.user_introdu)
    TextView tIntrodu;//简介
    @BindView(R.id.user_branch)
    TextView tBranch;//部门
    @BindView(R.id.user_version)
    TextView tVersion;//部门
    @BindView(R.id.user_assistant)
    TextView tAssis;//部门
    private int userid;

    @Override
    protected int layout() {
        return R.layout.frag_user_center;
    }

    @Override
    protected void init() {
        UGlide.loadImage(context, R.mipmap.bg_usercenter, iBg);
    }

    @Override
    protected void logic() {
        userid = SPUtil.getUserId();
        DbInfo info = HelperInfoDb.SearchById(context, SPUtil.getUserId());
        if (HelperInfoDb.checkEmpty(info)) {
            getUserInfo();
        } else {
            UGlide.loadAvatar(context, UTxt.isNull(info.getAvatar()), iAvatar);
            tName.setText(UTxt.isNull(info.getNameUser(), "------"));
            tBranch.setText(UTxt.isNull(info.getNameDept(), "---------------"));
            tIntrodu.setText(UTxt.isNull(info.getNameHost(), "------------------------------------------"));
            int job = info.getCodeJob();
            tJob.setText(UTxt.isNull(Logic.getFldJobTitle(job)));
            //助理医师
            if (job == 0) {
                tAssis.setVisibility(View.GONE);
            } else {
                tAssis.setVisibility(View.VISIBLE);
            }
        }
        tVersion.setText(JUtils.getAppVersionName());
    }

    private void getUserInfo() {
        ProgressDialog.getInstance().startProgressDialog(getActivity());
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("id", userid);
        UHttp.start(getServes().getUserInfo(jsonObject), "获取我的资料", new HttpMsgListener<ResultBean<UserInfoBean>>() {
            @Override
            public void onSubscribe(Disposable disposable) {
                addEvent(disposable);
            }

            @Override
            public void onError(String describe) {
                showNotice(describe);
            }

            @Override
            public void onFinish() {
                ProgressDialog.getInstance().stopProgressDialog();
            }

            @Override
            public void onSuccess(ResultBean<UserInfoBean> dto) {
                if (dto.isSuccess()) {
                    UserInfoBean bean = dto.getResult();
                    if (bean != null) {
                        UGlide.loadAvatar(context, UTxt.isNull(bean.getFldHeadPortrait()), iAvatar);
                        tName.setText(UTxt.isNull(bean.getFldCustomerName(), "???"));
                        tBranch.setText(UTxt.isNull(bean.getFldDeptName(), "???"));
                        tIntrodu.setText(UTxt.isNull(bean.getFldHosName(), "???"));
                        int title = bean.getFldJobTitle();
                        tJob.setText(UTxt.isNull(Logic.getFldJobTitle(title)));
                        //助理医师
                        if (title == 0) {
                            tAssis.setVisibility(View.GONE);
                        } else {
                            tAssis.setVisibility(View.VISIBLE);
                        }
                        HelperInfoDb.update(context, userid, bean);
                    }
                }
            }
        });
    }

    @OnClick({R.id.user_qrcode, R.id.user_scheduling, R.id.user_assistant,
        R.id.user_msg, R.id.user_out, R.id.user_info_lay, R.id.user_bg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.user_bg://我的资料:
            case R.id.user_info_lay://我的资料:
                if (userid == 0) {
                    showNotice("登录失效，请重新登录");
                    return;
                }
                startActivity(new Intent(context, UserInfoActivity.class));
                break;
            case R.id.user_qrcode://去二维码
                if (userid == 0) {
                    showNotice("登录失效，请重新登录");
                    return;
                }
                startActivity(new Intent(context, UserCodeActivity.class));
                break;
            case R.id.user_scheduling://我的排班:
                if (userid == 0) {
                    showNotice("登录失效，请重新登录");
                    return;
                }
                startActivity(new Intent(context, SchedulingActivity.class));
                break;
            case R.id.user_assistant://我的助理:
                if (userid == 0) {
                    showNotice("登录失效，请重新登录");
                    return;
                }
                startActivity(new Intent(context, AssistantMyActivity.class));
                break;
            case R.id.user_msg://我的消息:
                if (userid == 0) {
                    showNotice("登录失效，请重新登录");
                    return;
                }
                startActivity(new Intent(context, MyMsgActivity.class));
                break;
            case R.id.user_out://退出:
                UDialog.getInstance(context, true, true)
                    .showNoticeWithTwobtn("即将退出登录", (result, dia) -> {
                        MyUtil.getInstance().removeAuthor(context);
                    });
                break;
        }
    }

    @Override
    protected void refresh() {

    }
}
